var SLIDER_SPEED = 4000;

$(document).ready(function() {

	Slider.init();

});

var Slider = {
	slides: null,
	current: null,
	cursors: null,
	timer: null,
	
	init: function() {
		Slider.slides = $('#carrousel #visu a.visu');
		Slider.cursors = $('#carrousel #circles a');
		Slider.current = 0;
		Slider.printSlide(Slider.current);
		
		Slider.resetTimer();
		
		Slider.cursors.hover(function() {
			Slider.changeSlide(parseInt($(this).attr('class')));
		});
		
		$('#carrousel').hover(function() {
			Slider.pauseTimer();
		}, function() {
			Slider.resetTimer();
		});
		
		$('#carrousel').hover(function() {
			$('#carrousel .previousslide').fadeIn('slow');
			$('#carrousel .nextslide').fadeIn('slow');
		}, function() {
			$('#carrousel .previousslide').fadeOut('slow');
			$('#carrousel .nextslide').fadeOut('slow');
		});
		
		$('#carrousel .previousslide').click(function(){
			Slider.resetTimer();
			if (Slider.current!=0){
				Slider.changeSlide(Slider.current - 1);
			}
		});
		$('#carrousel .nextslide').click(function(){
			Slider.resetTimer();
			Slider.changeSlide(Slider.current + 1);
		});
		
	}, 
	
	printSlide: function() {
		$(Slider.slides).addClass('hide');
		$(Slider.slides[Slider.current]).removeClass('hide');
		$(Slider.cursors).removeClass('selected');
		$(Slider.cursors[Slider.current]).addClass('selected');
	},
	
	changeSlide: function(nb) {
		if(nb >= Slider.slides.length) nb = 0;
		Slider.current = nb;
		Slider.printSlide();
		Slider.resetTimer();
	},
	
	resetTimer: function() {
		clearTimeout(Slider.timer);
		Slider.timer = setTimeout(function() { Slider.changeSlide(Slider.current + 1); }, SLIDER_SPEED);
	},
	
	pauseTimer: function() {
		clearTimeout(Slider.timer);
	}
};